package connector;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import java.io.*;
import java.util.Locale;

/*
HTTP/1.1 200 OK
 */
public class Response implements ServletResponse {

  private static final int BUFFER_SIZE = 1024;

  Request request;
  OutputStream output;

  public Response(OutputStream output) {
    this.output = output;
  }

  public void setRequest(Request request) {
    this.request = request;
  }

  public void sendHeaders() throws IOException {
    output.write(ConnectorUtils.renderStatus(HttpStatus.SC_OK).getBytes());
  }

  public void sendStaticResource() throws IOException {
    String uri = request.getRequestURI();
    // 静态资源在 html 子目录中
    File file = new File(ConnectorUtils.WEB_ROOT, "html" + uri);
    try {
      write(file, HttpStatus.SC_OK);
    } catch (IOException e) {
      write(new File(ConnectorUtils.WEB_ROOT, "html/404.html"), HttpStatus.SC_NOT_FOUND);
    }
  }

  private void write(File resource, HttpStatus status) throws IOException {
    try (FileInputStream fis = new FileInputStream(resource)) {
      output.write(ConnectorUtils.renderStatus(status).getBytes());
      byte[] buffer = new byte[BUFFER_SIZE];
      int length = 0;
      while ((length = fis.read(buffer, 0, BUFFER_SIZE)) != -1) {
        output.write(buffer, 0, length);
      }
    }
  }

  @Override
  public String getCharacterEncoding() {
    return null;
  }

  @Override
  public String getContentType() {
    return null;
  }

  @Override
  public ServletOutputStream getOutputStream() throws IOException {
    return null;
  }

  @Override
  public PrintWriter getWriter() throws IOException {
    PrintWriter writer = new PrintWriter(output, true);
    return writer;
  }

  @Override
  public void setCharacterEncoding(String s) {

  }

  @Override
  public void setContentLength(int i) {

  }

  @Override
  public void setContentLengthLong(long l) {

  }

  @Override
  public void setContentType(String s) {

  }

  @Override
  public void setBufferSize(int i) {

  }

  @Override
  public int getBufferSize() {
    return 0;
  }

  @Override
  public void flushBuffer() throws IOException {

  }

  @Override
  public void resetBuffer() {

  }

  @Override
  public boolean isCommitted() {
    return false;
  }

  @Override
  public void reset() {

  }

  @Override
  public void setLocale(Locale locale) {

  }

  @Override
  public Locale getLocale() {
    return null;
  }
}